<?php
require 'Model.php';
class VideoModel extends Model{

function getVideoList(){
	$sql="select * from videos";
	$data=$this->conn->query($sql);
	return $data;
}
function delvideo($vid){
	$sql="delete from videos where vid=$vid";
	$result=$this->conn->query($sql);
	return $result;

}
function addvideo($data,$file){
//文件上传
$arr=explode('.', $file['pic']['name']);
$suffix=$arr[count($arr)-1];
$newname=date("YmdHis").rand(100,999).'.'.$suffix;
$filepath='./posters/';
	
move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
//把数据插入数据库
$videoname=$data['videoname'];
$videotype=$data['videotype'];
$address=$data['address'];
$intro=$data['intro'];
$pic=$newname;
$sql="insert into videos values(null,'$videoname',$videotype,'$pic','$intro',now(),0,0,0,'$address')";

echo $sql;
$result=$this->conn->query($sql);
	return $result;
}
function editvideo($vid){
$sql="select * from videos where vid=$vid";
$data=$this->conn->query($sql);
return $data;
}

function updatevideo($data,$file){
//接受数据
	$vid=$data['vid'];
	$videoname=$data['videoname'];
$videotype=$data['videotype'];
$address=$data['address'];
$intro=$data['intro'];
//如果修改了头像，需要上传新头像，否则原头像不动
if ($file['pic']['size']==0) {
	$sql = "update videos set videoname='$videoname', tid=$videotype, intro='$intro',address='$address' where vid=$vid";	
}
else{
//上传头像并且更新根据uid更新数据
$arr=explode('.', $file['pic']['name']);
$suffix=$arr[count($arr)-1];
$newname=date("YmdHis").rand(100,999).'.'.$suffix;
$filepath='./posters/';
	
move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);	
$sql = "update videos set videoname='$videoname', tid=$videotype, intro='$intro',address='$address',pic='$newname' where vid=$vid";	

}
echo $sql;
$result=$this->conn->query($sql);
	return $result;
}
}
 ?>
